Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
• Each Visual Basic project has a Forms collection that contains all the loaded forms in your project. • Each form in your project has a Controls collection that contains all of the controls on the form. • Windows maintains a Printers collection that contains a list of all the printers installed on the current system. I’ll cover the Printers collection in Chapter 12. You will most often use the Forms and Controls collections with the For Each...Next statement to access each and every form in your project or control on a form. The following code will populate a List Box control with the captions of all the project’s forms:
    Dim f As Form
    For Each f In Forms
        List1.AddItem f.Caption
    Next
Remember that the Forms collection contains only those forms that have been loaded. A program’s main form is loaded when the program starts. Other forms are loaded either explicitly with the Load statement or when referenced in code. Here’s another example that moves all of a program’s forms up and to the left by a small amount:
    Dim f As Form
    For Each f In Forms
        f.Top = f.Top * 0.8
        f.Left = f.Left * 0.8
    Next
You can also loop through the Forms collection using the indexes of the members, as shown here:
    Dim i As Integer
    For i = 0 to Forms.Count - 1
        Forms(i).Top = Forms(i).Top * 0.8
        Forms(i).Left = Forms(i).Left * 0.8
    Next i
This approach, however, has no advantage over the For Each statement, and in fact, the syntax is a bit messier. Because the order of the forms within the collection is maintained by Visual Basic, you should not write code that depends on the forms being in any specific order in the Forms collection. You can loop through the Controls collection in a similar manner. You must specify which form’s Controls collection you are referring to; otherwise, Visual Basic assumes it is the current form (the one containing the code). Here is code that will switch all the controls on the current form from visible to invisible, then back again, when executed another time:
    Dim c As Control
    For Each c In Controls
        c.Visible = Not c.Visible
    Next
You can combine looping through the Controls collection with looping through the Forms collection to access all of the controls in your project:
    Dim f As Form
    Dim c As Control

    For Each f In Forms
        For Each c In f.Controls
            ‘Do something with c here.
        Next
    Next
Looping though the controls can be more useful when you use the TypeOf keyword. This keyword lets you determine the type of a control—Text Box, Command Button, and so on. Then, you can apply code only to certain controls. For example, the following code clears all Text Box controls on a form:
    Dim c As Control
    For Each c in Controls
        If TypeOf c Is TextBox Then c.Text = “”
    Next
Likewise, this code disables all Command Buttons on all forms:
    Dim f As Form
    Dim c As Control

    For Each f In Forms
        For Each c In f.Controls
            If TypeOf c Is CommandButton Then c.Enabled = False

        Next
    Next
You can refer to the Visual Basic Help system for details on the TypeOf names associated with different objects. ### The Windows Common Controls Among the most useful controls available to you as a Visual Basic programmer are the Windows Common Controls. As their name suggests, these controls are not part of Visual Basic per se but rather are a part of the Windows operating system. Because they are ActiveX controls, however, they are available for use by any application running under Windows. In fact, you have probably already seen some of these controls in use. Windows Explorer, for example, uses at least three of them. Surprisingly, even some experienced Visual Basic programmers are unaware that they can use the Windows Common Controls in their projects. Perhaps this is because these controls are not part of the intrinsic controls, so they do not show up in the toolbox unless you put them there. Once you know about these controls, I think you’ll use them frequently. One possible pitfall, however, is that some of the Common Controls are rather complex, and figuring out how to use them is not nearly as easy to figure out as, say, a Text Box or Option Button control. I have seen programmers turn away from these controls in frustration. That’s too bad, because the effort they will save you in the long run is well worth the effort required to master them. To help you get started with the Windows Common Controls, I have devoted the remainder of this chapter to the fundamentals of using them. Space limitations make it impossible to cover them all, or even to cover some of them in complete detail. I have, therefore, taken the following approach: 1. I will start by explaining the ImageList control, as it is used by several of the other controls. 2. I will explain the TreeView control in considerable detail. It is the most complex of all the common controls. Once you are familiar with it, learning the other controls will be an easier task. 3. I will explain the TabStrip and ListView controls in less detail, because some of what you learn about the TreeView control will apply here as well. What about the other Common Controls? I will deal with the Toolbar control in a later chapter, when we use it in the database project. The remaining Windows Common Controls—Slider, Progress Bar, ImageCombo, and Status Bar—are not too complex, so you should be able to figure them out on your own. Remember, to make these controls available in your project, you must display the Components dialog box (Ctrl+T) and put a checkmark next to Microsoft Windows Common Controls. Once you do this, the nine controls will be displayed in your toolbox. #### ImageList Control The ImageList control is designed to let you create and manage a group of images. This control always works “behind the scenes”; you never actually see it when your program is running. It does not display images, but rather it is used to make a set of images available to other controls that use them, such as the TabStrip control discussed later in this chapter. An ImageList control can also perform certain types of manipulations on the images it contains, such as overlaying one image on another. While there is no restriction on the size of images placed in an ImageList control, it is generally used for small images, such as icons. An ImageList control contains a collection called ListImages containing one or more ListImage objects. As with all collections, each object is identified by a 1-based Index property, giving its position in the collection, as well as by an optional Key property. You can add images to an ImageList control at design time or runtime. At design time, display the control’s property pages and select the Images tab. Browse your disk for the image file, and optionally specify a Key property for the image. Repeat until you have added all the required images. At runtime, you use the Add method to add images to the ImageList control. The syntax is similar to the Add method for other collections. Because we are dealing with loading a picture from a disk file, however, you must use the LoadPicture function. The following example adds the image APPLE.BMP to the ImageList and assigns it a Key property of “apple”.
    ImageList1.ListImages.Add , “apple”, LoadPicture(“apple.bmp”)
While all the images loaded into an ImageList control do not have to be the same size, the control constrains them all to the same size once they are in the control. This makes sense, because for most uses, you would want all the images to appear as the same size when they are displayed—for example, on the tabs of a TabStrip control. What determines this size? At design time, you can specify the image size on the general page in the ImageList’s property pages. This is permitted only if the control contains no images at the time. Otherwise, the native size of the first image loaded into the control will be used for all other images.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy |